﻿package com.sgcc.nfc.lock.presentation.region

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemRegionBinding
import com.sgcc.nfc.lock.domain.model.Region

class RegionAdapter(
    private val typeLabelProvider: (RegionListItem) -> String,
    private val onToggleExpand: (RegionListItem) -> Unit,
    private val onAddChild: (Region) -> Unit,
    private val onEdit: (Region) -> Unit,
    private val onDelete: (Region) -> Unit
) : ListAdapter<RegionListItem, RegionAdapter.RegionViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RegionViewHolder {
        val binding = ItemRegionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return RegionViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RegionViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class RegionViewHolder(private val binding: ItemRegionBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: RegionListItem) {
            val context = binding.root.context
            val params = binding.root.layoutParams as ViewGroup.MarginLayoutParams
            params.marginStart = (item.depth * INDENT_DP).dp(context)
            binding.root.layoutParams = params

            binding.regionNameText.text = item.region.regionName
            binding.typeChip.text = typeLabelProvider(item)

            val hasChildren = item.hasChildren
            binding.expandIcon.isVisible = hasChildren
            binding.expandIcon.rotation = if (item.isExpanded) 180f else 0f
            binding.expandIcon.contentDescription = context.getString(
                if (item.isExpanded) R.string.common_collapse else R.string.common_expand
            )
            binding.expandIcon.setOnClickListener {
                if (hasChildren) onToggleExpand(item)
            }

            binding.actionsGroup.isVisible = item.canManage
            binding.addChildButton.isEnabled = item.canAddChild
            binding.addChildButton.alpha = item.canAddChild.toAlpha()
            binding.editButton.isEnabled = item.canEdit
            binding.editButton.alpha = item.canEdit.toAlpha()
            binding.deleteButton.isEnabled = item.canDelete
            binding.deleteButton.alpha = item.canDelete.toAlpha()

            binding.addChildButton.setOnClickListener {
                if (item.canAddChild) onAddChild(item.region)
            }
            binding.editButton.setOnClickListener {
                if (item.canEdit) onEdit(item.region)
            }
            binding.deleteButton.setOnClickListener {
                if (item.canDelete) onDelete(item.region)
            }
        }
    }

    private fun Int.dp(context: android.content.Context): Int =
        (this * context.resources.displayMetrics.density).toInt()

    private fun Boolean.toAlpha(): Float = if (this) 1f else DISABLED_ALPHA

    private object DiffCallback : DiffUtil.ItemCallback<RegionListItem>() {
        override fun areItemsTheSame(oldItem: RegionListItem, newItem: RegionListItem): Boolean =
            oldItem.region.regionId == newItem.region.regionId

        override fun areContentsTheSame(oldItem: RegionListItem, newItem: RegionListItem): Boolean =
            oldItem == newItem
    }

    companion object {
        private const val INDENT_DP = 24
        private const val DISABLED_ALPHA = 0.35f
    }
}

